---
title: Storage Configuration
description: Configure storage backends for memory persistence
---

LangCrew Memory supports multiple storage providers for both short-term and long-term memory. Choose the appropriate provider based on your deployment environment and scalability needs.

## Overview

LangCrew Memory supports multiple storage providers:

- **✅ Production-Ready**: `memory`, `sqlite`, `postgresql`, `mysql`
- **⚠️ Experimental**: `redis`, `mongodb`

## Storage Providers

### In-Memory Storage

Fast, ephemeral storage for development and testing. No persistence across restarts.

```python
from langcrew.memory import MemoryConfig

memory_config = MemoryConfig(
    provider="memory",  # No persistence
    short_term={"enabled": True},
    long_term={"enabled": True},
)
```

**Characteristics:**
- **Speed**: Fastest
- **Persistence**: None (lost on restart)
- **Best for**: Development, testing, ephemeral sessions

### SQLite

File-based database ideal for single-user applications and development.

```python
memory_config = MemoryConfig(
    provider="sqlite",
    connection_string="sqlite:///memory.db",
    short_term={"enabled": True},
    long_term={"enabled": True},
)
```

**Connection String Examples:**

```python
# Relative path
"sqlite:///memory.db"

# Absolute path
"sqlite:////var/data/memory.db"

# In-memory SQLite (testing only)
"sqlite:///:memory:"
```

**Characteristics:**
- **Speed**: Fast
- **Persistence**: File-based
- **Best for**: Single-user apps, development, local deployments

### PostgreSQL

Scalable, production-ready database with full ACID compliance.

```python
# PostgreSQL configuration
memory_config = MemoryConfig(
    provider="postgresql",
    connection_string="postgresql://user:password@localhost:5432/memory_db",
    short_term={"enabled": True},
    long_term={"enabled": True},
)
```

**Connection String Examples:**

```python
# Basic connection
"postgresql://user:password@localhost:5432/database"

# With specific schema
"postgresql://user:password@localhost:5432/database?schema=memory"

# With connection pooling
"postgresql://user:password@localhost:5432/database"

# Using environment variables
f"postgresql://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@{os.getenv('DB_HOST')}:5432/{os.getenv('DB_NAME')}"
```

**Characteristics:**
- **Speed**: Good (network-dependent)
- **Persistence**: Durable, ACID-compliant
- **Best for**: Production, multi-user applications, scalable deployments

**Required Setup:**

```sql
-- PostgreSQL requires specific extensions
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS hstore;
```

### MySQL

Production-ready relational database with wide adoption.

```python
memory_config = MemoryConfig(
    provider="mysql",
    connection_string="mysql://user:password@localhost:3306/memory_db",
    short_term={"enabled": True},
    long_term={"enabled": True},
)
```

**Characteristics:**
- **Speed**: Good (network-dependent)
- **Persistence**: Durable
- **Best for**: Production, existing MySQL infrastructure

### Redis (⚠️ Experimental)

High-performance in-memory data store with optional persistence.

```python
memory_config = MemoryConfig(
    provider="redis",
    connection_string="redis://localhost:6379/0",
    short_term={"enabled": True},
    long_term={"enabled": True},
)
```

**Connection String Examples:**

```python
# Basic Redis connection
"redis://localhost:6379/0"

# Redis with authentication
"redis://:password@localhost:6379/0"

# Redis with custom database
"redis://localhost:6379/2"
```

**⚠️ Status**: Experimental - not fully tested in production environments.

### MongoDB (⚠️ Experimental)

Document-oriented NoSQL database.

```python
memory_config = MemoryConfig(
    provider="mongodb",
    connection_string="mongodb://localhost:27017/memory_db",
    short_term={"enabled": True},
    long_term={"enabled": True},
)
```

**⚠️ Status**: Experimental - not fully tested in production environments.

## Environment-based Configuration

Configure different storage providers for different environments:

```python
import os

def get_memory_config():
    env = os.getenv("ENVIRONMENT", "development")
    
    if env == "production":
        return MemoryConfig(
            provider="postgresql",
            connection_string=os.getenv("DATABASE_URL"),
            short_term={"enabled": True},
            long_term={"enabled": True},
        )
    elif env == "staging":
        return MemoryConfig(
            provider="postgresql",
            connection_string="postgresql://staging_user:pass@staging-db:5432/memory_staging",
            short_term={"enabled": True},
            long_term={"enabled": True},
        )
    else:  # development
        return MemoryConfig(
            provider="sqlite",
            connection_string="sqlite:///dev_memory.db",
            short_term={"enabled": True},
            long_term={"enabled": True},
        )

# Usage
memory_config = get_memory_config()
```

## Security Best Practices

### Use Environment Variables

```python
# Good: Environment variables
connection_string = os.getenv("DATABASE_URL")

# Bad: Hardcoded credentials
connection_string = "postgresql://admin:password123@localhost:5432/db"
```

### Secure Connection Strings

```python
# PostgreSQL with SSL
"postgresql://user:pass@localhost:5432/db?sslmode=require"

# MySQL with SSL
"mysql://user:pass@localhost:3306/db?ssl=true"
```

## Troubleshooting

### Connection Failures

- Verify database is running and accessible
- Check connection string format
- Ensure database permissions are correct
- Verify the database file/connection is accessible

### Performance Issues

- Monitor database resource usage
- Optimize database indices if needed
- Consider using PostgreSQL for production workloads with high concurrency

## Next Steps

- **[Short-term Memory](/guides/memory/short-term)** - Configure conversation history
- **[Long-term Memory](/guides/memory/long-term)** - Configure persistent knowledge
- **[Memory Concepts](/concepts/memory)** - Understand memory architecture
